﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LTSMZ.life.Model;
using System.Data.SqlClient;
using System.Data;

namespace LTSMZ.life.DAL
{
    public class CommentDal
    {
        Qianli.DirSir.Common.SQLHelper sql = new Qianli.DirSir.Common.SQLHelper();
        /// <summary>
        /// 点击商品,下面显示本商品的评论
        /// </summary>
        /// <param name="OrderId">商品ID</param>
        /// <returns></returns>
        public List<Comment> ShowComment(int OrderId)
        {
            string str = "select Comment.Content,Comment.CommentTime,Member.NickName from Comment join Member on Member.Mid=Comment.Mid join Product on Product.Pid=Comment.Pid where Comment.Pid="+OrderId+"";
            //StringBuilder str = new StringBuilder();
            //str.Append("select c.CommentID,c.CommentTime,c.Content,c.NickName from(");
            //str.Append("select a.CommentID,a.CommentTime,a.Content,b.NickName,a.Pid from Comment a join Member b on a.Mid=b.Mid)");
            //str.Append("c join Product d on c.Pid=d.Pid  where d.Pid=@OrderId");
            //SqlParameter[] param = new SqlParameter[]
            //{
            //    new SqlParameter("@OrderId",SqlDbType.Int,10)
            //};
            //param[0].Value=OrderId;
            DataTable table = sql.ExecDataTable(str);
            List<Comment> li = new List<Comment>();
            foreach (DataRow i in table.Rows)
            {
                Comment o = new Comment();
                o.Content = i["Content"].ToString();
                o.CommentTime = Convert.ToDateTime(i["CommentTime"]).ToString();
                o.NickName = i["NickName"].ToString();
                //o.Member.NickName = i["NickName"].ToString();
                li.Add(o);
            }
            return li;
        }
        /// <summary>
        /// 显示当前商品的总评论数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int ShowCount(int id)
        {
            string str = "select COUNT(*) from Comment join Product on Product.Pid=Comment.Pid where Comment.Pid="+id+"";
            return Convert.ToInt32(sql.ExecScalar(str));
        }
        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="com">评论表相关信息</param>
        /// <param name="users">用户代号</param>
        /// <param name="Pid">商品代号</param>
        /// <returns></returns>
        public int AddComment(Comment com, int users, int Pid)
        {
            string str = "insert into Comment(Mid,Pid,Content,CommentTime) values(@Mid,@Pid,@Content,@CommentTime)";
           // StringBuilder str = new StringBuilder();
           // str.Append("insert into Comment(Mid,Pid,Content,CommentTime)");
           // str.Append(" values(");
           // str.Append("@Mid,@Pid,@Content,@CommentTime)");
           // SqlParameter[] param = new SqlParameter[]
           // {
           //    new SqlParameter("@Mid",SqlDbType.Int,10),
           //    new SqlParameter("@Pid",Pid),
           //    new SqlParameter("@Content",com.Content),
           //    new SqlParameter("@CommentTime",com.CommentTime)
           //};
            //param[0].Value = users;
            //param[1].Value = Pid;
            //param[2].Value = com.Content;
            //param[3].Value = com.CommentTime;
            int i = 0;
            i = sql.ExecNonquery(str.ToString());
            return i;
        }
    }
}
